package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.ILocalQuerySceneOrderAttrService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.PreSalesInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.service.util.CopyToDtoUtil;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 售前信息模块
 */
@Service
public class PreSalesInfoModuleHandler extends AbstractModuleHandler {

    @Autowired
    ILocalQuerySceneOrderAttrService querySceneOrderAttrService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        PreSalesInfoVO salesInfoVO = new PreSalesInfoVO();
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String offerInstId = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId()).getRootInstId();

        List<KiteAttr> commonAttrList = querySceneInstService.listCommonAttrs(context.getSceneVo().getSceneInstId(), "44");
        List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(commonAttrList);
        // 没有售前属性信息返回空，不展示售前信息导航
        if (KiteListUtils.isEmpty(instAttrDTOList)) {
            return Lists.newArrayList();
        }
        salesInfoVO.setSalesAttr(instAttrDTOList);
        salesInfoVO.setOfferInstId(offerInstId);
        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        sceneAccountInfoVo.setPresaleInfo(salesInfoVO);

        return Collections.singletonList(salesInfoVO);
    }

    @Override
    public List<ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String custId = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId()).getCustId();
        String offerInstId = querySceneInstService.getSceneInst(context.getAcceptRequest().getSceneInstId()).getRootInstId();
        List<KiteAttr> commonAttrList = querySceneInstService.listCommonAttrs(context.getSceneVo().getSceneInstId(), "44");
        //设置预览模式
        commonAttrList.forEach((attr) -> {
            attr.setAttrValueTypeId("TT");
        });
        PreSalesInfoVO orderInfoVO = new PreSalesInfoVO();

        List<InstAttrDTO> instAttrDTOList = CopyToDtoUtil.transInstAttrDtoList(commonAttrList);
        // 没有售前属性信息返回空，不展示售前信息导航
        if (KiteListUtils.isEmpty(instAttrDTOList)) {
            return Lists.newArrayList();
        }
        orderInfoVO.setSalesAttr(instAttrDTOList);
        orderInfoVO.setOfferInstId(offerInstId);
        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        sceneAccountInfoVo.setPresaleInfo(orderInfoVO);
        return Collections.singletonList(orderInfoVO);
    }

}
